#define  _CRT_SECURE_NO_WARNINGS
#include <queue>
using namespace std;


struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
 
class Solution {
public:
    bool isCompleteTree(TreeNode* root) {
        if (!root) return true;
        queue<TreeNode*> st;
        st.push(root);
           
        bool flag = false;
        while (!st.empty())
        {
            int n = st.size();
            while (n--)
            {
                TreeNode* node = st.front();
                st.pop();
                if (flag && (node->left || node->right)) return false;

                if (node->left) st.push(node->left);
                else flag = true;

                if (flag && (node->left || node->right)) return false;

                if (node->right) st.push(node->right);
                else flag = true;
            }
        }
        return true;
    }
};

int main()
{
    TreeNode* node = new TreeNode(1);
    node->left = new TreeNode(2);
    node->right = new TreeNode(3);
    node->left->left = new TreeNode(4);
    node->left->right = new TreeNode(5);
    node->right->left = new TreeNode(6);
    Solution().isCompleteTree(node);


}